package com.leapp.sk.viewdelegate.im

import android.content.res.Resources
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.util.TypedValue
import android.view.View
import android.widget.LinearLayout
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.SKTopBar
import com.leapp.sk.R
import com.leapp.sk.bean.TopLayoutTitleEntity
import com.leapp.sk.ui.activity.im.MessageStateActivity
import com.leapp.sk.ui.activity.notify.NotifyActivity
import java.lang.reflect.Field

/**
 * 已读未读消息状态展示界面视图托管
 * @author  chaohaipeng  on 2018/7/26.
 */
class MessageStateDelegate : BaseAppDelegate(){
    /**
     * fragment数据集合
     */
  private  var fragmentLists:List<Fragment> ?=null
    /**
     * 标签集合
     */
    private var titles:List<TopLayoutTitleEntity>?=null
    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_message_state
    }
    /**
     * 设置标题
     */
    fun setTitle(title:String?){
        val topBar= getView<SKTopBar>(R.id.jtb_ams)
        topBar.inVisibleLine()
        if (!TextUtils.isEmpty(title)){
            topBar?.setTitle(title)
        }
        setEvent(topBar)
    }
    /**
     * 设置设置事件
     */
    private  fun setEvent(topBar:SKTopBar){
        topBar?.setEventInterface(object : SKTopBar.EventInterface{
            override fun leftOnClick() {
                getActivity<MessageStateActivity>().finish()

            }
            override fun rightOnClick() {
            }

        })
    }
    /**
     * 初始化Tablayout
     */
    fun initTabLayout(activity: AppCompatActivity,fragmentLists:List<Fragment>,titles:List<TopLayoutTitleEntity>){
         if (fragmentLists.size==titles.size){
             this.fragmentLists= fragmentLists
             this.titles=titles
             val tab_ams_toblayout = getView<TabLayout>(R.id.tab_ams_toblayout)
             val vp_ams_container = getView<ViewPager>(R.id.vp_ams_container)
             val mFragmentAdapteradapter = FragmentAdapter(activity.supportFragmentManager, fragmentLists)
             vp_ams_container?.adapter=mFragmentAdapteradapter
             tab_ams_toblayout?.post(Runnable { setIndicator(tab_ams_toblayout, 60, 60) })
             //将TabLayout和ViewPager关联起来。
             tab_ams_toblayout?.setupWithViewPager(vp_ams_container)
         }

    }

    /**
     * FragmentAdapter
     */
    private inner class FragmentAdapter(fm: FragmentManager, private val mFragments: List<Fragment>) : FragmentPagerAdapter(fm) {

        override fun getItem(position: Int): Fragment {
            return mFragments[position]
        }

        override fun getCount(): Int {
            return mFragments.size
        }

        override fun getPageTitle(position: Int): CharSequence {
            return titles?.get(position)?.title.toString()
        }
    }

    /**
     * 选项卡设置
     */
    private  fun setIndicator(tabs: TabLayout, leftDip: Int, rightDip: Int) {
        val tabLayout = tabs.javaClass
        var tabStrip: Field? = null
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip")
        } catch (e: NoSuchFieldException) {
            e.printStackTrace()
        }

        tabStrip!!.isAccessible = true
        var llTab: LinearLayout? = null
        try {
            llTab = tabStrip.get(tabs) as LinearLayout
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        }

        val left = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip.toFloat(), Resources.getSystem().displayMetrics).toInt()
        val right = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip.toFloat(), Resources.getSystem().displayMetrics).toInt()

        for (i in 0 until llTab!!.childCount) {
            val child = llTab.getChildAt(i)
            child.setPadding(0, 0, 0, 0)
            val params = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1f)
            params.leftMargin = left
            params.rightMargin = right
            child.layoutParams = params
            child.invalidate()
        }

    }

}